package com.difsonapps.android.marker.services;

import java.util.ArrayList;
import java.util.List;

import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.listeners.OnInviteListener;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class InviteFacebookFriendsIntentService extends IntentService {
	SimpleFacebook mSimpleFacebook;
	// TODO: Rename actions, choose action names that describe tasks that this
	// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
	private static final String ACTION_INVITEFRIENDS = "com.difsonapps.android.marker.action.INVITEFRIENDS";

	// TODO: Rename parameters
	private static final String EXTRA_FRIENDS_IDS = "com.difsonapps.android.marker.extra.FRIENDS_IDS";
	public static final String NOTIFICATION = "com.difsonapps.android.marker.services.InviteFacebookFriendsIntentService.receiver";
	public static final String NB_INVITATIONS = "com.difsonapps.android.marker.services.InviteFacebookFriendsIntentService.NB_INVITATIONS";
	public static final String RESULT = "com.difsonapps.android.marker.services.InviteFacebookFriendsIntentService.RESULT";

	/**
	 * Starts this service to perform action Foo with the given parameters. If
	 * the service is already performing a task this action will be queued.
	 * 
	 * @see IntentService
	 */
	// TODO: Customize helper method
	public static void startActionInviteFriend(Context context, ArrayList<String> friendsIds) {
		Intent intent = new Intent(context, InviteFacebookFriendsIntentService.class);		
		intent.setAction(ACTION_INVITEFRIENDS);
		intent.putExtra(EXTRA_FRIENDS_IDS, friendsIds);
		context.startService(intent);
	}

	public InviteFacebookFriendsIntentService() {
		super("InviteFacebookFriendsIntentService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		if (intent != null) {
			final String action = intent.getAction();
			if (ACTION_INVITEFRIENDS.equals(action)) {
				final ArrayList<String> friendsIds = intent.getStringArrayListExtra(EXTRA_FRIENDS_IDS);
				handleActionInviteFriends(friendsIds);
			}
		}
	}

	/**
	 * Handle action Foo in the provided background thread with the provided
	 * parameters.
	 */
	private void handleActionInviteFriends(ArrayList<String> friendsIds) {
		if(mSimpleFacebook==null) mSimpleFacebook = SimpleFacebook.getInstance();
		//Activity activity = (Activity)this.getBaseContext();
		//if(mSimpleFacebook==null) mSimpleFacebook = SimpleFacebook.getInstance(activity);
		Log.d(this.getClass().getCanonicalName(), "Handling service...");
		int nb = 0;
		String[] t = new String[]{};
		mSimpleFacebook.invite(friendsIds.toArray(t), "I invite you to use this app", onInviteListener, null);
		//TODO Invite friends and send a broadcast receiver 
		/*Intent intent = new Intent(NOTIFICATION);
	    intent.putExtra(NB_INVITATIONS, nb);
	    intent.putExtra(RESULT, Activity.RESULT_OK);
	    sendBroadcast(intent);
	    Log.d(this.getClass().getCanonicalName(), "Broadcast sent");*/
	}
	
	OnInviteListener onInviteListener = new OnInviteListener() {
	    @Override
	    public void onComplete(List<String> invitedFriends, String requestId) {
	    	Intent intent = new Intent(NOTIFICATION);
		    intent.putExtra(NB_INVITATIONS, invitedFriends.size());
		    intent.putExtra(RESULT, Activity.RESULT_OK);
		    sendBroadcast(intent);
		    Log.d(this.getClass().getCanonicalName(), "Broadcast sent");
		    
	        Log.i(this.getClass().getCanonicalName(), "Invited friend " + invitedFriends.toString() + " ; users with request id " + requestId); 
	    }

	    @Override
	    public void onCancel() {
	        Log.i(this.getClass().getCanonicalName(), "Canceled the dialog");
	    }

		@Override
		public void onException(Throwable throwable) {
			Log.e(this.getClass().getCanonicalName(), throwable.getMessage());
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onFail(String reason) {
			Log.w(this.getClass().getCanonicalName(), reason);
			// TODO Auto-generated method stub
			
		}

	    /* 
	     * You can override other methods here: 
	     * onFail(String reason), onException(Throwable throwable)
	     */
	};
}
